﻿Imports Microsoft.Build.Framework
Imports Microsoft.Build.Tasks
Imports Microsoft.Build.Utilities
Imports System
Imports System.Collections.Generic
Imports System.IO

Namespace System.Workflow.ComponentModel.Compiler
    Friend NotInheritable Class CreateWorkflowManifestResourceNameForCSharp
        Inherits CreateCSharpManifestResourceName
        ' Methods
        Protected Overrides Function CreateManifestName(ByVal fileName As String, ByVal linkFileName As String, ByVal rootNamespace As String, ByVal dependentUponFileName As String, ByVal binaryStream As Stream) As String
            Dim metadataValue As String = String.Empty
            If Not Me.lastAskedFileWasXoml Then
                metadataValue = MyBase.CreateManifestName(fileName, linkFileName, rootNamespace, dependentUponFileName, binaryStream)
            Else
                metadataValue = TasksHelper.GetXomlManifestName(fileName, linkFileName, rootNamespace, binaryStream)
            End If
            Dim extension As String = Path.GetExtension(fileName)
            If ((String.Compare(extension, ".rules", StringComparison.OrdinalIgnoreCase) = 0) OrElse (String.Compare(extension, ".layout", StringComparison.OrdinalIgnoreCase) = 0)) Then
                metadataValue = (metadataValue & extension)
            End If
            Dim item As New TaskItem(fileName)
            If Not String.IsNullOrEmpty(dependentUponFileName) Then
                item.SetMetadata("DependentUpon", dependentUponFileName)
            End If
            item.SetMetadata("ManifestResourceName", metadataValue)
            If (Me.resourceFilesWithManifestResourceNames Is Nothing) Then
                Me.resourceFilesWithManifestResourceNames = New List([Of] ITaskItem)
            End If
            Me.resourceFilesWithManifestResourceNames.Add(item)
            Me.lastAskedFileWasXoml = False
            Return metadataValue
        End Function

        Protected Overrides Function IsSourceFile(ByVal fileName As String) As Boolean
            If (String.Compare(Path.GetExtension(fileName), ".xoml", StringComparison.OrdinalIgnoreCase) = 0) Then
                Me.lastAskedFileWasXoml = True
                Return True
            End If
            Return MyBase.IsSourceFile(fileName)
        End Function


        ' Properties
        <Output> _
        Public ReadOnly Property ResourceFilesWithManifestResourceNames As ITaskItem()
            Get
                Return Me.resourceFilesWithManifestResourceNames.ToArray
            End Get
        End Property


        ' Fields
        Private lastAskedFileWasXoml As Boolean
        Private resourceFilesWithManifestResourceNames As List([Of] ITaskItem)
    End Class
End Namespace

